package com.example.yeujian5.yuejian5.model;

import com.example.yeujian5.yuejian5.Interface.ClubFace;
import com.example.yeujian5.yuejian5.Listenner.ActivityListener;
import com.example.yeujian5.yuejian5.Listenner.ClubDetailListener;
import com.example.yeujian5.yuejian5.Listenner.ClubListener;

import com.example.yeujian5.yuejian5.Listenner.ClubNearListener;
import com.example.yeujian5.yuejian5.Listenner.SearchListener;
import com.example.yeujian5.yuejian5.bean.ClubBean;

import com.example.yeujian5.yuejian5.service.ClubService;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangmengmeng on 2017/9/30.
 */

public class ClubModel implements ClubFace {

    private Retrofit retrofit;
    private ClubService clubService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public ClubModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())//设置解析方式
                .build();
    }

    /**
     * 获得列表

     * @param listListener 监听对象
     */
    @Override
    public void getList(String id, String club_name,final ClubListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<List<ClubBean>> call =clubService.getClubList();
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    @Override
    public void getLists(String id, String club_name,final ClubNearListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<List<ClubBean>> call =clubService.ClubList();
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    //获取详情
    @Override
    public void getdetail(String id,final ClubDetailListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<ClubBean> call =clubService.getclubById(id);
        call.enqueue(new Callback<ClubBean>() {
            @Override
            public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<ClubBean> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    //搜索
    @Override
    public void getsearch(String club_name,final SearchListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<List<ClubBean>>  call =clubService.getSearchClub(club_name);
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    //收藏俱乐部
    @Override
    public void CollectClub( String SessionID,String userid, String fan_id, final ActivityListener.ClubCollectListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Integer> call =clubService.CollectClub("lgj98rfkiqvenhrk2o7oe2po81",userid,fan_id,"1");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() > 0) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    public retrofit2.Call<Integer> getClubState(String fan_id,String userid){
        ClubService clubService = retrofit.create(ClubService.class);
        return  clubService.getClubState(fan_id,userid,"1","lgj98rfkiqvenhrk2o7oe2po81");
    }


    //获取俱乐部课程
    @Override
    public void getclass(String id,final ClubDetailListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<ClubBean> call =clubService.getclassinfo("lgj98rfkiqvenhrk2o7oe2po81","1","Clubcourse","club",id);
        call.enqueue(new Callback<ClubBean>() {
            @Override
            public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<ClubBean> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
}
